#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "uart.h"
#include "os_api.h"
#include "time_api.h"
#include "debug.h"
#include "ps_api.h"
#include "dev_api.h"


#define _STACK_SIZE			0x2000
static void* _task_stack = NULL;

static OSTaskRef _task_ref = NULL;


OSATimerRef _task_delete_timer_ref = NULL;

static void _task(void *ptr);

static void wait_network_ready(void)
{
    int count = 30;
    while(count-- && ps_GetNetworkReady() == FALSE){	
        OSATaskSleep(200);
    }

    if (count <= 0){
        LOG_PRINTF("dev_CtrlReset.\n");
        dev_CtrlReset(0);
    }
}

extern int mqtt_basic_demo(
    char* mqtt_host, 
    char* product_key, 
    char* device_name, 
    char* device_secret, 
    char* dynregmq_devicesecret, 
    char* dynregmq_clientid, 
    char* dynregmq_username, 
    char* dynregmq_password,      
    int register_mdoe);

extern int dynreg_basic_demo(
    char *product_key, 
    char *device_name, 
    char *product_secret, 
    char *dynreg_devicesecret);

extern int dynregmq_basic_demo(
    char* mqtt_host, 
    char* product_key, 
    char* device_name, 
    char* product_secret, 
    char* dynregmq_devicesecret, 
    char* dynregmq_clientid, 
    char* dynregmq_username, 
    char* dynregmq_password,   
    int skip_pre_regist_flag);


char *aliyun_mqtt_host       = "a1aoLjDNOU0.iot-as-mqtt.cn-shanghai.aliyuncs.com";

/* TODO: 一机一密三元组 */
char *product_key_a       = "a1aoLjDNOU0";
char *device_name_a       = "test_00001";
char *device_secret_a     = "56ef59b2d8539a82f1f40d2d5a9ad6a4";

/* TODO: 一型一密三元组，预注册 */
char *product_key_b       = "a1aoLjDNOU0";
char *device_name_b       = "test_00003";
char *product_secret_b     = "Em9W9jqP0q6h06wL";

/* TODO: 一型一密三元组，免注册 */
char *product_key_c       = "a1aoLjDNOU0";
char *device_name_c       = "test_00005";
char *product_secret_c     = "Em9W9jqP0q6h06wL";


static void _task(void *ptr)
{
    int ret;
    //int mode = 0;
    //int mode = 1;
    int mode = 2;

	wait_network_ready();

    LOG_PRINTF("network ready\n");

	if (mode == 0){
        ret = mqtt_basic_demo(aliyun_mqtt_host, product_key_a, device_name_a, device_secret_a, NULL, NULL, NULL, NULL, 0);
		LOG_PRINTF("mqtt_basic_demo mode 0: ret %d\n", ret);
    }else if (mode == 1){
        char dynregmq_devicesecret[128] = {0};
        //ret = dynreg_basic_demo(product_key_b, device_name_b, product_secret_b, dynregmq_devicesecret);
        ret = dynregmq_basic_demo(aliyun_mqtt_host, product_key_b, device_name_b, product_secret_b, dynregmq_devicesecret, NULL, NULL, NULL, 0);
        LOG_PRINTF("dynreg_basic_demo: ret %d, dynregmq_devicesecret, %s\n", ret, dynregmq_devicesecret);
        ret = mqtt_basic_demo(aliyun_mqtt_host, product_key_b, device_name_b, NULL, dynregmq_devicesecret, NULL, NULL, NULL, 1);
		LOG_PRINTF("mqtt_basic_demo mode 1: ret %d\n", ret);        
    }else if (mode == 2){        
        char dynregmq_clientid[128] = {0};
        char dynregmq_username[128] = {0};
        char dynregmq_password[128] = {0};

        ret = dynregmq_basic_demo(aliyun_mqtt_host, product_key_c, device_name_c, product_secret_c, NULL, dynregmq_clientid, dynregmq_username, dynregmq_password, 1);
        LOG_PRINTF("dynreg_basic_demo: ret %d, dynregmq, %s, %s, %s\n", ret, dynregmq_clientid, dynregmq_username, dynregmq_password);
        ret = mqtt_basic_demo(aliyun_mqtt_host, NULL, NULL, NULL, NULL, dynregmq_clientid, dynregmq_username, dynregmq_password, 2);
		LOG_PRINTF("mqtt_basic_demo mode 2: ret %d\n", ret);  
    }

}

int main(int argc, char const *argv[])
{
     int ret;
	
	_task_stack = malloc(_STACK_SIZE);
	
    ret = OSATaskCreate(&_task_ref, _task_stack, _STACK_SIZE, 100, "_task", _task, NULL);
    ASSERT(ret == OS_SUCCESS);
    return 0;
}

